﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using Common.Library;
using System.Text.RegularExpressions;

namespace Social_Celebrities.Views
{
    public partial class Browser_Blogger : PhoneApplicationPage
    {
        //IDictionary<string, string> UrlList = new Dictionary<string, string>();
        string Parameter = string.Empty;
        string BlogLink = string.Empty;
        string Link_Blog = string.Empty;
        
        public Browser_Blogger()
        {
            InitializeComponent();
            Loaded += Browser_Blogger_Loaded;
        }

        void Browser_Blogger_Loaded(object sender, RoutedEventArgs e)
        {
            Browser_Blog.Navigate(new Uri("https://www.google.co.in/search?q=" + Parameter + " blog", UriKind.RelativeOrAbsolute));
        }

        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            if (NavigationContext.QueryString.TryGetValue("Parameter", out Parameter))
            {
            }
        }

        private void Browser_Blog_LoadCompleted(object sender, NavigationEventArgs e)
        {
            Copy_AddrBar_Content.Visibility = Visibility.Collapsed;
            AddressBar.Text = e.Uri.AbsoluteUri;
            if (AddressBar.Text.EndsWith(".com/") || AddressBar.Text.EndsWith(".in/") || AddressBar.Text.Contains("blogspot.in/") || AddressBar.Text.Contains("blogspot.com/") || AddressBar.Text.Contains("tumblr.com/") ||
                AddressBar.Text.Contains("tumblr.in/") || AddressBar.Text.Contains("wordpress.com/") || AddressBar.Text.Contains("wordpress.in/"))
            {
                Copy_AddrBar_Content.Visibility = Visibility.Visible;
            }
            else
            {
                //Copy_AddrBar_Content.Visibility = Visibility.Collapsed;
                //Browser_Blog.Navigate(new Uri("https://www.google.co.in/search?q=" + Parameter + " blog", UriKind.RelativeOrAbsolute));
                Cancel.Visibility = Visibility.Visible;
            }
        }        

        private void Copy_AddrBar_Content_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                if (AddressBar.Text.Contains("blogspot.in/") || AddressBar.Text.Contains("blogspot.com/") || AddressBar.Text.Contains("tumblr.com/") ||
                    AddressBar.Text.Contains("tumblr.in/") || AddressBar.Text.Contains("wordpress.com/") || AddressBar.Text.Contains("wordpress.in/"))
                {
                    BlogLink = AddressBar.Text;
                    if (BlogLink.EndsWith(".com/") || BlogLink.EndsWith(".in/"))
                    {
                        Constants.BlogAddress = BlogLink;
                        NavigationService.GoBack();
                    }
                    else
                    {
                        var url = new Uri(BlogLink);
                        Link_Blog = url.Host;
                        if (!Link_Blog.EndsWith("/"))
                        {
                            Link_Blog = Link_Blog + "/";
                        }
                        //Link_Blog = BlogLink.Substring(0, BlogLink.IndexOf("?"));
                        //Link_Blog = BlogLink.Substring(0, BlogLink.LastIndexOf('/') + 1).ToString();
                        //Link_Blog = BlogLink.Length - 1 <= BlogLink.LastIndexOf('/') ? BlogLink : BlogLink.Substring(0, BlogLink.LastIndexOf('/') + 1);
                        Constants.BlogAddress = Link_Blog;
                        NavigationService.GoBack();
                    }
                }
                else
                {
                    MessageBox.Show("Not a Valid Blog Address..!!!  TRY AGAIN");
                    Browser_Blog.Navigate(new Uri("https://www.google.co.in/search?q=" + Parameter + " blog", UriKind.RelativeOrAbsolute));
                }
                AppSettings.BackgroundAgenError = "Image_MouseLeftButtonDown_1 Method Has Completed Successfully...!!!!";
            }
            catch (Exception ex)
            {
                //Exceptions.SendErrortoMail("Exception at Image_MouseLeftButtonDown_1 Method in Browser_Blogger.xaml.cs Page and the error is " + " " + ex.Message + " " + ex.StackTrace);
                Exceptions.SaveOrSendExceptions("Exception in Image_MouseLeftButtonDown_1 method in Browser_Blogger.xaml.cs file..!!", ex);
                AppSettings.BackgroundAgenError = ex.Message;
            }
        }

        private void Cancel_Click(object sender, RoutedEventArgs e)
        {
            Browser_Blog.Navigate(new Uri("https://www.google.co.in/search?q=" + Parameter + " blog", UriKind.RelativeOrAbsolute));
        }
    }
}